#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from sqlalchemy import Column, String, Integer, Float, create_engine, BigInteger
from sqlalchemy.ext.declarative import declarative_base

# 创建对象的基类:
Base = declarative_base()


# 定义User对象:
class User(Base):
    # 表的名字:
    __tablename__ = 'user'

    # 表的结构:
    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(String(20))
    account = Column(String(11))
    password = Column(String(45))
    balance = Column(Float)
    status = Column(Integer)


class Driver(Base):
    __tablename__ = 'driver'
    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(String(20))
    account = Column(String(11))
    password = Column(String(45))
    balance = Column(Float)
    carType = Column(String(20))
    carCode = Column(String(20))
    status = Column(Integer)


class Order(Base):
    __tablename__ = 'order'
    id = Column(Integer, autoincrement=True, primary_key=True)
    uid = Column(Integer)
    sourceName = Column(String(50))
    sourceLatitude = Column(Float)
    sourceLongitude = Column(Float)
    destName = Column(String(50))
    destLatitude = Column(Float)
    destLongitude = Column(Float)
    did = Column(Integer)
    driverLatitude = Column(Float)
    driverLongitude = Column(Float)
    status = Column(Integer)
    start_time = Column(BigInteger)
    end_time = Column(BigInteger)

    def to_dict(self):
        return {
            'orderid': self.id,
            'uid': self.uid,
            'sourceName': self.sourceName,
            'sourceLatitude': self.sourceLatitude,
            'sourceLongitude': self.sourceLongitude,
            'destName': self.destName,
            'destLatitude': self.destLatitude,
            'destLongitude': self.destLongitude,
            'did': self.did,
            'driverLatitude': self.driverLatitude,
            'driverLongitude': self.driverLongitude,
            'startTime': self.start_time,
            'endTime': self.end_time,
            'status': self.status
        }


# 初始化数据库连接:
engine = create_engine('sqlite:///data.db', echo=True)
Base.metadata.reflect(engine)
Base.metadata.create_all(engine)
